package com.dianjing.dianqun.api.dianpai;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.dianjing.dianqun.common.ResponseData;
import com.dianjing.dianqun.common.base.BaseRestController;
import com.dianjing.dianqun.utils.FastDFSClient;
import com.dianjing.dianqun.utils.FileUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @date : 2017年10月24日
 * 
 * @author: baoyongtao
 * 
 * @see :
 * 
 */
@RestController
@RequestMapping("/api/file")
@Component
@Api(value = "FileUploadApiController", description = "文件上传相关api")
public class FileUploadApiController extends BaseRestController {

	@Value("${fdfs.host}")
	private String host;

	@ApiOperation(value = "文件上传", notes = "文件上传")
	@PostMapping(value = "/upload", produces = { "application/json" })
	public ResponseData multipart(@RequestParam("image") MultipartFile[] multipartfiles) throws Exception {
		List<String> list = new ArrayList<String>(multipartfiles.length);
		if (null != multipartfiles && multipartfiles.length > 0) {
			// 遍历并保存文件
			for (MultipartFile multipartFile : multipartfiles) {
				String fid = FastDFSClient.uploadFile(multipartFile.getBytes(),
						FileUtils.getFileExtension(multipartFile.getOriginalFilename()), null);
				System.out.println(fid);
				list.add(host + fid);
			}
		}

		return new ResponseData(ResponseData.STATUS_OK, "上传完成", list);
	}

}
